package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 对品牌的分页查询 排序以及动态查询
     *
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @param key
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<BrandDTO>> pageQuery(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", required = false) Boolean desc,
            @RequestParam(value = "key", required = false) String key) {

        return ResponseEntity.ok(this.brandService.pageQuery(page, rows, sortBy, desc, key));
    }


    /**
     * 品牌新增
     * @param brandDTO，简易写法，在编译时，编译工具，可以根据类中的getter，setter方法动态生成，@requestParam，但是加了属性requried=false
     * @param cids 本质是字符串，但是springMvc的消息转换器，可以把具有某种特征的字符串，转换为集合
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(
            BrandDTO brandDTO,
            @RequestParam("cids") List<Long> cids) {
        this.brandService.addBrand(brandDTO, cids);

        //返回201，表示新增成功
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    @PutMapping
    public ResponseEntity<Void> updateBrand(
            BrandDTO brandDTO,
            @RequestParam("cids") List<Long> cids) {
        this.brandService.updateBrand(brandDTO, cids);

        //返回200
        return ResponseEntity.ok().build();
    }

    /**
     * 根据分类的id，查询对应的品牌集合
     * @param cid
     * @return
     */
    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(@RequestParam("id")Long cid){

        return ResponseEntity.ok(this.brandService.queryBrandByCategory(cid));
    }

    /**
     * 根据品牌的id集合查询对应的品牌集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids")List<Long> ids){

        return ResponseEntity.ok(this.brandService.queryBrandByIds(ids));
    }

    /**
     * 根据品牌id查询对应的品牌对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id")Long id){

        return ResponseEntity.ok(this.brandService.queryBrandById(id));
    }
}
